// 创建面板
chrome.devtools.panels.create(
  "API录制器",
  null,
  "devtools/panel.html",
  function(panel) {
    console.log("DevTools面板创建成功");
  }
);

// 监听网络请求
chrome.devtools.network.onRequestFinished.addListener(request => {
  // 只处理POST请求
  if (request.request.method !== 'POST') return;
  
  const url = request.request.url;
  
  // 获取请求体
  let requestBody = '';
  if (request.request.postData) {
    requestBody = request.request.postData.text || '';
    
    // 尝试格式化JSON
    try {
      const jsonData = JSON.parse(requestBody);
      requestBody = JSON.stringify(jsonData, null, 2);
    } catch (e) {
      // 不是JSON，保持原样
    }
  }
  
  // 获取响应体
  request.getContent((content, encoding) => {
    let responseBody = content;
    
    // 尝试格式化JSON
    try {
      const jsonData = JSON.parse(responseBody);
      responseBody = JSON.stringify(jsonData, null, 2);
    } catch (e) {
      // 不是JSON，保持原样
    }
    
    // 发送消息到background.js
    chrome.runtime.sendMessage({
      type: 'captureCompleteRequest',
      url: url,
      requestBody: requestBody,
      responseBody: responseBody
    });
  });
}); 